//
//  AyfeatureController.m
//  xWeibo
//
//  Created by alvis mac on 8/6/15.
//  Copyright (c) 2015 alvis. All rights reserved.
//

#import "AyfeatureController.h"
#import "UIImageExtend.h"
#import "AyPageControl.h"
#import "AyDefine.h"
#import "AyTableBarController.h"

#define AyNewfeatureImageCount 3

@interface AyfeatureController ()<UIScrollViewDelegate>
@property (nonatomic, weak)  UIPageControl * pageControl;
@end

@implementation AyfeatureController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupScrollView];
    [self setupPageControl];
   
}

- (void)setupPageControl
{
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = AyNewfeatureImageCount;
    CGFloat centerX = self.view.frame.size.width * 0.5;
    CGFloat centerY = self.view.frame.size.height - 30;
    pageControl.center = CGPointMake(centerX, centerY);
    pageControl.bounds = CGRectMake(0, 0, 100, 30);
    pageControl.userInteractionEnabled = NO;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
    
    pageControl.currentPageIndicatorTintColor = AyColor(253, 98, 42);
    pageControl.pageIndicatorTintColor = AyColor(189, 189, 189);
}

- (void) setupScrollView{

    UIScrollView *scrollView=[[UIScrollView alloc] init];
    scrollView.frame=self.view.bounds;
    scrollView.delegate=self;
    [self.view addSubview:scrollView];
    
    CGFloat imageW = scrollView.frame.size.width;
    CGFloat imageH = scrollView.frame.size.height;
    for (int index=0; index<AyNewfeatureImageCount; index++) {
        UIImageView * fImageView=[[UIImageView alloc] init];
        fImageView.frame=CGRectMake(index*imageW, 0, imageW, imageH);
        NSString *name = nil;
        if (fourInch) {
            name = [NSString stringWithFormat:@"new_feature_%d-568h", index + 1];
        }else{
             name = [NSString stringWithFormat:@"new_feature_%d", index + 1];
        }
        fImageView.image = [UIImage imageWithName:name];
        [scrollView addSubview:fImageView];
    };
    

    scrollView.contentSize=CGSizeMake(imageW*AyNewfeatureImageCount, imageH);
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.pagingEnabled=YES;
    scrollView.bounces=NO;
    
    [self setupLastImageView:[scrollView.subviews lastObject]];
  
}

- (void) setupLastImageView:(UIImageView *)imageView{
    
    imageView.userInteractionEnabled=YES;
    
    //Button
    UIButton *startButton=[[UIButton alloc] init];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    
    CGFloat centerX=imageView.frame.size.width/2;
    CGFloat centerY=imageView.frame.size.height/2;
    startButton.center=CGPointMake(centerX, centerY);
    startButton.bounds=(CGRect){CGPointZero,startButton.currentBackgroundImage.size};
    
    [startButton setTitle:@"开始微博" forState:UIControlStateNormal];
    [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startButton];
    
    
    //CheckBox
    UIButton *checkbox = [[UIButton alloc] init];
    checkbox.selected=YES;
    [checkbox setTitle:@"分享给大家" forState:UIControlStateNormal];
    [checkbox setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];
    [checkbox setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];
    checkbox.bounds = CGRectMake(0, 0, 200, 50);
    CGFloat checkboxCenterX = centerX;
    CGFloat checkboxCenterY = imageView.frame.size.height * 0.5-40;
    checkbox.center = CGPointMake(checkboxCenterX, checkboxCenterY);
    [checkbox setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    checkbox.titleLabel.font = [UIFont systemFontOfSize:15];
    [checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
    checkbox.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
    [imageView addSubview:checkbox];


}

- (void) start{
    [UIApplication sharedApplication].statusBarHidden = NO;
    self.view.window.rootViewController = [[AyTableBarController alloc] init];

}

- (void) checkboxClick:(UIButton *) sender{
    sender.selected=!sender.selected;
}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX=scrollView.contentOffset.x;
    
    double pageDouble=offsetX/scrollView.frame.size.width;
    int pageInt=(int) (pageDouble+0.5);
    self.pageControl.currentPage=pageInt;

}

@end
